/*
 * Created on Nov 6, 2007
 */
package sheep.view.Component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.jdesktop.fuse.InjectedResource;
import org.jdesktop.fuse.ResourceInjector;

/**
 * @author Daniel Spiewak
 */
public class StyledDropDown extends JComponent {
	private String[] items;
	private int selectedIndex = 0;
	
	@InjectedResource
	private Color borderColor, highlightColor, textColor, textAccent, textAccentHot, divider;
	
	@InjectedResource
	private GradientPaint topGradientUp, topGradientDown, bottomGradientUp, bottomGradientDown;
	
	@InjectedResource
	private int width, height, padding;
	
	@InjectedResource
	private Image arrow;
	
	private boolean pushed;
	
	public StyledDropDown(String[] items) {
		this.items = items;
		
		if (items.length < 0) {
			throw new RuntimeException("Lazy coder alert!!  Can't handle 0 length items");
		}
		
		ResourceInjector.get().inject(this);
		
		addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
				pushed = false;
				repaint();
			}

			@Override
			public void mousePressed(MouseEvent e) {
				pushed = true;
				repaint();
				
				EventQueue.invokeLater(new Runnable() {
					@Override
					public void run() {
						showMenu();
					}
				});
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				pushed = false;
				repaint();
			}
		});
	}
	
	private void showMenu() {
		final JPopupMenu menu = new JPopupMenu();
		
		for (int i = 0; i < items.length; i++) {
			JMenuItem menuItem = new JMenuItem(items[i]);
			
			final int currentIndex = i;
			menuItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					selectedIndex = currentIndex;
					pushed = false;
					repaint();
					
					menu.setVisible(false);
				}
			});
			menu.add(menuItem);
		}

		menu.setLocation(getLocationOnScreen().x, getLocationOnScreen().y + getHeight());
		menu.setPreferredSize(new Dimension(Math.max(menu.getPreferredSize().width, getWidth()),
				menu.getPreferredSize().height));
		menu.setOpaque(false);
		menu.setVisible(true);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		int height = getHeight() / 2;
		
		g2.setPaint(pushed ? topGradientDown : topGradientUp);
		g2.fillRect(0, 0, getWidth(), height);
		
		g2.setPaint(pushed ? bottomGradientDown : bottomGradientUp);
		g2.fillRect(0, height, getWidth(), height);
		
		g2.setColor(borderColor);
		g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
		
		g2.setColor(highlightColor);
		g2.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
		
		int x = padding / 2;
		int y = getHeight() - ((getHeight() - g2.getFontMetrics().getHeight()) / 2) - 3;
		
		y += 1;
		g2.setColor(pushed ? textAccentHot : textAccent);
		g2.drawString(getText(), x, y);
		
		y -= 1;
		g2.setColor(textColor);
		g2.drawString(getText(), x, y);
		
		x = getWidth() - (arrow.getWidth(this) + padding) - 1;
		y = 4;
		
		g2.setColor(divider);
		g2.drawLine(x, y, x, getHeight() - 5);
		
		x += padding / 2;
		y = (getHeight() - arrow.getHeight(this)) / 2;
		
		g2.drawImage(arrow, x, y, this);
	}
	
	private String getText() {
		return items[selectedIndex];
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(width, height);
	}
	
	public String[] getItems() {
		return items;
	}
	
	public void setItems(String[] items) {
		this.items = items;
	}
	
	public boolean isPushed() {
		return pushed;
	}
}
